package com.wangzeqi.changliaoba.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wangzeqi.changliaoba.R;
import com.wangzeqi.changliaoba.base.BaseActivity;

/**
 * Created by dllo on 16/2/29.
 */
public class GuidePageActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    //装点点的ImageView数组
    private ImageView[] tips;
    // 装ImageView数组
    private ImageView[] mImageViews;
    //图片id
    private int[] imgIdArray;
    private TextView textView;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sharedPreferences = getSharedPreferences("first_enter", MODE_PRIVATE);
        if (sharedPreferences.getBoolean("start", false)) {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
            return;
        } else {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("start", true);
            editor.commit();

        }

        setContentView(R.layout.activity_guidepage);
        ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
        viewPager = (ViewPager) findViewById(R.id.guide_page);
        imgIdArray = new int[]{R.mipmap.post_login_viewpager_item1,
                R.mipmap.post_login_viewpager_item2,
                R.mipmap.post_login_viewpager_item3};
        textView = (TextView) findViewById(R.id.pass_guide_page);
        //将点点加入到ViewGroup中
        tips = new ImageView[imgIdArray.length];
        for (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new LayoutParams(10, 10));
            tips[i] = imageView;
            if (i == 0) {
                tips[i].setBackgroundResource(R.drawable.dot_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.dot_normal);
            }
            LinearLayout.LayoutParams layoutParmas = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            layoutParmas.leftMargin = 5;
            layoutParmas.rightMargin = 5;
            group.addView(imageView, layoutParmas);
        }
        //将图片装载到数组中
        mImageViews = new ImageView[imgIdArray.length];
        for (int i = 0; i < mImageViews.length; i++) {
            ImageView imageView = new ImageView(this);
            mImageViews[i] = imageView;
            imageView.setBackgroundResource(imgIdArray[i]);

        }
        viewPager.setAdapter(new GuidePageAdapter());
        viewPager.setOnPageChangeListener(this);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_pass = new Intent(GuidePageActivity.this, EnterActivity.class);
                startActivity(intent_pass);
                finish();
            }
        });

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setImageBackground(position % mImageViews.length);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.drawable.dot_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.dot_normal);
            }
        }
    }


    public class GuidePageAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return imgIdArray.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        /**
         * 当只有3张图片或者2张图片的时候，滑动存在BUG问题的修改，
         * 注释掉 ((ViewPager) container).removeView(mImageViews[position % mImageViews.length]);
         * 抛出异常
         * destroyItem(View container, int position, Object object)方法中不removeView
         */
        @Override
        public void destroyItem(View container, int position, Object object) {
//            ((ViewPager) container).removeView(mImageViews[position % mImageViews.length]);
        }

        // 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
        @Override
        public Object instantiateItem(View container, int position) {
            //instantiateItem(View container, int position)增加异常扑捉
            try {
                ((ViewPager) container).addView(mImageViews[position % mImageViews.length], 0);
            } catch (Exception e) {

            }
            return mImageViews[position % mImageViews.length];
        }
    }

}
